﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using com.cruxomatic.notifications.email.DTOs;

namespace com.cruxomatic.notifications.Configuration
{
    public class SettingManager
    {
        private XDocument _ConfigurationDocument = null;
        public SettingManager(XDocument ConfigurationDocument)
        {
            _ConfigurationDocument = ConfigurationDocument;
        }

        public EmailNotificationActionInformation GetEmailNotificationSettings(string ActionName)
        {
            XDocument cruxConfigurationDocument = _ConfigurationDocument;
            XElement notificationsNode = cruxConfigurationDocument.Element("CruxConfigurations")
                                         .Element("Notifications")
                                         .Element("EmailNotifications");
            EmailNotificationInformation emailNotificationInformation
                = GetEmailNotificationInformation(notificationsNode);

            XElement actionNotificationNode = notificationsNode.Element("NotificationActions")
                                               .Elements("Action")
                                               .SingleOrDefault(p => p.Attribute("Name").Value == ActionName);
            EmailNotificationActionInformation emailActionNotification = GetEmailNotificationActionInformation
                (actionNotificationNode);

            emailActionNotification.EmailNotificationInformation = emailNotificationInformation;

            return emailActionNotification;
        }

        private EmailNotificationInformation GetEmailNotificationInformation(XElement EmailNotificiationNode)
        {
            EmailNotificationInformation emailNotificationInformation = new EmailNotificationInformation();
            emailNotificationInformation.Notify = bool.Parse(EmailNotificiationNode.Attribute("Notify").Value);
            emailNotificationInformation.UseSSL = bool.Parse(EmailNotificiationNode.Attribute("UseSSL").Value);
            emailNotificationInformation.UserName = EmailNotificiationNode.Attribute("UserName").Value;
            emailNotificationInformation.EmailPassword = EmailNotificiationNode.Attribute("Password").Value;
            emailNotificationInformation.Port = int.Parse(EmailNotificiationNode.Attribute("Port").Value);
            emailNotificationInformation.SmtpServer = EmailNotificiationNode.Attribute("SmtpServer").Value;
            emailNotificationInformation.FromAddress = EmailNotificiationNode.Attribute("FromAddress").Value;
            emailNotificationInformation.SendInHtml = bool.Parse(EmailNotificiationNode.Attribute("SendInHtml").Value);
            emailNotificationInformation.TemplateFolder = EmailNotificiationNode.Attribute("TemplateFolder").Value;
            return emailNotificationInformation;
        }


        private EmailNotificationActionInformation GetEmailNotificationActionInformation(XElement EmailNotificationActionNode)
        {
            EmailNotificationActionInformation actionNotification = new EmailNotificationActionInformation();
            actionNotification.ActionName = EmailNotificationActionNode.Attribute("Name").Value;
            actionNotification.Notify = bool.Parse(EmailNotificationActionNode.Attribute("Notify").Value);
            actionNotification.NotificationTemplateFile = EmailNotificationActionNode.Attribute("NotificationTemplateFile").Value;
            actionNotification.MailSubject = EmailNotificationActionNode.Attribute("MailSubject").Value;
            return actionNotification;
        }

        public string GetApplicationPath()
        {
            XDocument cruxConfigurationDocument = _ConfigurationDocument;
            return cruxConfigurationDocument.Element("CruxConfigurations")
                .Element("ApplicationSettings")
                .Element("GeneralSettings")
                .Element("ApplicationPath").Attribute("URL").Value;
        }
    }
}
